package com.baoxin.chenxixc4coach.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.GlideApp;
import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.TrainStudent;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class TrainStudentAdapter extends RecyclerView.Adapter<TrainStudentAdapter.ViewHolder> {
    private List<TrainStudent> studentList;
    private Context context;
    private TrainStuClickListener listener;

    public TrainStudentAdapter(Context context, TrainStuClickListener listener) {
        this.context = context;
        this.listener = listener;
    }

    public void setData(List<TrainStudent> studentList) {
        this.studentList = studentList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.student_training_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        TrainStudent student = studentList.get(position);
        GlideApp.with(context)
                .load(student.getTrImg())
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .error(R.drawable.default_)
                .into(holder.icon);
        holder.name.setText(student.getTrName());
        holder.id.setText(student.getTrIdnumber());
        holder.enrollTime.setText(student.getTrCtime());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.showArrangeDialog(studentList.get(position));
            }
        });

    }

    @Override
    public int getItemCount() {
        return studentList == null ? 0 : studentList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        ImageView icon;
        TextView name;
        TextView id;
        TextView enrollTime;

        public ViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            icon = itemView.findViewById(R.id.student_icon);
            name = itemView.findViewById(R.id.student_name);
            id = itemView.findViewById(R.id.student_id);
            enrollTime = itemView.findViewById(R.id.enroll_time);
        }
    }

    public interface TrainStuClickListener {
        void showArrangeDialog(TrainStudent student);
    }
}
